var express = require('express');
var query = require("../util/dbHelper.js");
var router = express.Router();

router.post("/",(req,res,next)=> {
    let user = req.body.user;//查找改用户有没有地址
    req.myPromise = query("SELECT * FROM `dt_mi_site` WHERE `user` = ?;",[user])//调用sql函数，查询符合uName的
        .then(results => {
            return {status:200,message:"",data:results};
        });
    next();
});
router.post("/addSite",(req,res,next)=> {
    let {user,consignee,phone,site,activate,id} = req.body.data; //把收到的数据解构
    if(activate){//插入默认地址
        let act = 1;
        if(!req.body.data.none){//如果用户至少已经有一个地址了
            req.myPromise = query('UPDATE `dt_mi_site` SET activate = ? WHERE `activate` = ? AND `user` = ?;',[null,act,user])
            .then(result => {
                if(result.affectedRows === 1){//返回的数据库受影响的行
                    req.myPromise = query('INSERT `dt_mi_site`(`user`,`site`,`consignee`,`phone`,`activate`) VALUES(?,?,?,?,?)',[ user, site, consignee, phone,act])
                        .then(result => ({
                            status: 200,
                            message: '成功',
                        }));
                 }
            });
            next();
        }else{//如果用户没有地址
            req.myPromise = query('INSERT `dt_mi_site`(`user`,`site`,`consignee`,`phone`,`activate`) VALUES(?,?,?,?,?)',[ user, site, consignee, phone,act])
                .then(result => ({
                    status: 200,
                    message: '成功',
                }))
            next();    
        }
        
    }else if(activate === false){//插入非默认地址
        let act = null;
        req.myPromise = query('INSERT `dt_mi_site`(`user`,`site`,`consignee`,`phone`,`activate`) VALUES(?,?,?,?,?)',[ user, site, consignee, phone,act])
            .then(result => ({
                status: 200,
                message: '成功',
            }));
        next();
    }else{//切换地址
        req.myPromise = query('UPDATE `dt_mi_site` SET activate = ? WHERE `activate` = ? AND `user` = ?;',[null,1,user])
            .then(result => {
                req.myPromise = query('UPDATE `dt_mi_site` SET activate = ? WHERE `id` = ?;',[1,id])
                    .then(result => ({
                        status: 200,
                        message: '成功',
                    }))
            });
        next();
    }

});
router.post('/del',(req,res,next)=>{
    let id = req.body.id;//要删除的商品id
    let activate = req.body.activate;
    let i = req.body.i;
    req.myPromise = query("DELETE FROM `dt_mi_site` WHERE `id` = ?;",[id])//获取提交过来的id
        .then(result => {
            if(result.affectedRows === 1){//返回的数据库受影响的行
                if(activate && i !== null){
                    req.myPromise = query('UPDATE `dt_mi_site` SET activate = ? WHERE `id` = ?;',[1,i])
                        .then(result => {
                            if(result.affectedRows === 1){//返回的数据库受影响的行
                                return {state:200,message:"从新设置默认地址",data:null};
                            }else{
                                return {state:199,message:"默认地址设置失败",data:null};
                            }
                        });
                    next();
                }
                return {state:200,message:"删除成功",data:null};
            }
            else return{ state:199,message: "删除失败",data: null};
        });
    next();
});

module.exports = router;
